<?php


namespace app\mod\pcr\service;

use app\mod\pcr\model\PcrGveBoss;
use app\mod\pcr\model\PcrGveGroup;
use app\mod\pcr\model\PcrGveGroupFight;
use app\mod\pcr\model\PcrGveGroupMember;
use app\mod\pcr\model\PcrGveGroupStatus;

class PcrGveGroupService {

    /**
     * 创建新的公会
     * @param int $qq_group_id QQ群ID
     * @param int $owner_user_id 创建用户ID
     * @param string $name 公会名称
     */
    public function createGroup(int $qq_group_id, int $owner_user_id, string $name) {
        $pcr_gve_group = new PcrGveGroup();
        return $pcr_gve_group->insert([
            'qq_group_id' => $qq_group_id,
            'owner_user_id' => $owner_user_id,
            'name' => $name,
            'created_at' => time(),
            'status' => PcrGveGroup::STATUS_NORMAL,
        ]);
    }

    /**
     * 添加成员
     * users接受用户信息数组 {"id": 123, "nickname": "Micoya"}
     * @param int $group_id
     * @param int $user_id
     * @param string $nickname
     */
    public function addMember(int $group_id, int $user_id, string $nickname) {
        return (new PcrGveGroupMember())->insert([
            'group_id' => $group_id,
            'user_id' => $user_id,
            'nickname' => $nickname,
            'role' => 0,
            'created_at' => time(),
            'status' => PcrGveGroupMember::STATUS_NORMAL,
        ]);
    }


    public function getGroupByQQGroupID(int $qq_group_id) {
        return (new PcrGveGroup())->getByQQGroupId($qq_group_id);
    }

    public function getGroupStatus(int $group_id) {
        return (new PcrGveGroupStatus())->getByGroupId($group_id);
    }

    /**
     * 初始化公会主题BOSS状态
     * @param int $group_id
     * @param int $theme_id
     */
    public function initThemeStatus(int $group_id, int $theme_id) {
        $m = new PcrGveGroupStatus();
        $boss = (new PcrGveBoss())->getByThemeSort($theme_id, 1);
        if (empty($boss)) {
            throw new \Exception("错误的BOSS主题 theme_id: {$theme_id}, sort_id: 1");
        }
        $data = [
            'group_id' => $group_id,
            'boss_theme_id' => $theme_id,
            'boss_sort_id' => 1,
            'hp' => $boss['hp_r1'],
            'round' => 1,
            'locked' => 0,
            'locker_user_id' => 0,
        ];
        $status = $m->getByGroupId($group_id);
        if (empty($status)) {
            $m->insert($data);
        } else {
            foreach ($data as $_k => $_v) {
                $status[$_k] = $_v;
            }
            $status->save();
        }
    }

    public function attackBoss(int $group_id, $user_id, $attack, $origin_boss_hp, $type) {
        $model_fight = new PcrGveGroupFight();
        // 增加出刀记录
        $report = [
            'group_id' => $group_id,
            'user_id' => $user_id,
            'attack' => $attack,
            'origin_boss_hp' => $origin_boss_hp,
            'type' => $type,
            'created_at' => time(),
        ];
        $model_fight->insert($report);
    }

    public function nextBoss(int $group_id) {
        $status = $this->getGroupStatus($group_id);
        $boss_model = new PcrGveBoss();
        // 下一个BOSS
        $theme_id = $status['boss_theme_id'];
        $bosses = $boss_model->getListByThemeID($theme_id);
        // 如果当前sort_id == boss总数则进入下一周目
        if (count($bosses) === $status['boss_sort_id']) {
            $status['round'] = $status['round'] + 1;
            $status['boss_sort_id'] = 1;
        } else {
            // SORT + 1
            $status['boss_sort_id'] = $status['boss_sort_id'] + 1;
        }
        $boss = $boss_model->getByThemeSort($theme_id, $status['boss_sort_id']);
        if (is_null($boss)) {
            throw new \Exception("不存在的BOSS：theme_id={$theme_id}, sort_id={$status['boss_sort_id']}");
        }
        $status['hp'] = $boss['hp_r1'];
        $status->save();
    }

    public function cancelReport($report) {
        if ($report['type'] === PcrGveGroupFight::TYPE_END) {
            throw new \Exception("尾刀暂不允许撤销，请人工处理。");
        }
        $status = $this->getGroupStatus($report['group_id']);
        $status['hp'] = $report['origin_boss_hp'];
        $status->save();
        $report->delete();
    }

}
